﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace ProfilerPDS
{
    public sealed class Cross : Shape
    {
        #region Dependency Properties

        public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(double), typeof(Cross), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
        public static readonly DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(double), typeof(Cross), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));

        #endregion

        #region CLR Properties

        [TypeConverter(typeof(LengthConverter))]
        public double X
        {
            get { return (double)base.GetValue(XProperty); }
            set { base.SetValue(XProperty, value); }
        }

        [TypeConverter(typeof(LengthConverter))]
        public double Y
        {
            get { return (double)base.GetValue(YProperty); }
            set { base.SetValue(YProperty, value); }
        }

        #endregion

        #region Overrides
        //This is used internally by the Shape class to retrieve the geometry that the stroke and fill properties are applied to to produce the rendered graphic.
        protected override Geometry DefiningGeometry
        {
            get
            {
                // Create a StreamGeometry for describing the shape
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;

                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalDrawCrossGeometry(context);
                }

                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }

        #endregion

        #region Privates

        private void InternalDrawCrossGeometry(StreamGeometryContext context)
        {

            Point pt = new Point(X, Y);
            Point pt1 = new Point(X-10, Y-10);
            Point pt2 = new Point(X+10, Y-10);
            Point pt3 = new Point(X+10, Y+10);
            Point pt4 = new Point(X-10, Y+10);

            context.BeginFigure(pt, true, false);
            context.LineTo(pt1, true, true);
            context.LineTo(pt, true, true);
            context.LineTo(pt2, true, true);
            context.LineTo(pt, true, true);
            context.LineTo(pt3, true, true);
            context.LineTo(pt, true, true);
            context.LineTo(pt4, true, true);
            context.LineTo(pt, true, true);
        }

        #endregion
    }
}
